using Unity.VisualScripting;
using UnityEngine;

public class MoveWithObstacleAvoidance : MonoBehaviour
{
    [Header("移动参数")]
    [Tooltip("目标X坐标位置")]
    public float targetX = 5f;
    
    [Tooltip("水平移动速度")]
    public float moveSpeed = 2f;
    
    [Tooltip("垂直避障速度")] 
    public float avoidSpeed = 3f;
    
    [Tooltip("障碍检测距离")]
    public float raycastDistance = 0.1f;

    [Tooltip("障碍检测高度偏移")]
    public float raycastOffsety = 0.5f;
    [Tooltip("障碍检测宽度偏移")]
    public float raycastOffsetx = 0.5f;
    [Tooltip("等待时间")]
    public float WAIT_DURATION = 2f;
    [Tooltip("等待时间")]
    public bool isAvoiding = false;
    private float originalY;
    private bool isWaiting = false;
    private float waitTimer = 0f;
    
    private Vector3 lastPosition;
    private bool isMoving = false;
    public Rigidbody2D rb;

    void Start()
    {
        rb.GetComponent<Rigidbody2D>();
        originalY = transform.position.y;
        lastPosition = transform.position;
        // 初始检查是否需要等待
        if (!Mathf.Approximately(transform.position.x, targetX))
        {
            isWaiting = true;
        }
    }

    private float noMovementTimer = 0f;
    private const float NO_MOVEMENT_THRESHOLD = 0.5f;

    void Update()
    {
        // 检测是否在移动
        isMoving = Vector3.Distance(transform.position, lastPosition) > 0.001f;
        lastPosition = transform.position;

        // 等待逻辑 - 仅在水平移动时检查
        if (!isAvoiding)
        {
            if (isWaiting && !isAvoiding)  // 添加避障状态检查
            {
                waitTimer += Time.deltaTime;
                if (waitTimer >= WAIT_DURATION)
                {
                    isWaiting = false;
                }
                else
                {
                    return;
                }
            }
            else if (!isMoving && !Mathf.Approximately(transform.position.x, targetX))
            {
                isWaiting = true;
                waitTimer = 0f;
                return;
            }
        }

        float direction = Mathf.Sign(targetX - transform.position.x);
        
        // 左右移动时的障碍检测
        if (direction != 0)
        {
            Vector2 rayOrigin = new Vector2(
                transform.position.x + raycastOffsetx, 
                transform.position.y + raycastOffsety);
                
            Vector2 rayDirection = direction > 0 ? Vector2.right : Vector2.left;
            RaycastHit2D hit = Physics2D.Raycast(
                rayOrigin, 
                rayDirection, 
                raycastDistance);
                
            Debug.DrawRay(rayOrigin, rayDirection * raycastDistance, Color.red);
            if (hit.collider != null && hit.collider.gameObject != this.gameObject)
            {
                isAvoiding = true;
                Debug.Log("Obstacle detected!");
            }
            else
            {
                isAvoiding = false;
            }
        }

        if (isAvoiding)
        {
            float newY = transform.position.y + avoidSpeed * Time.deltaTime;
            rb.gravityScale = 0;
            transform.position = new Vector3(
                transform.position.x, 
                newY, 
                transform.position.z);
            
            // 重置无移动计时器
            noMovementTimer = 0f;
        }
        else
        {
            // 检查Y方向是否移动
            if (Mathf.Abs(transform.position.y - lastPosition.y) < 0.001f)
            {
                noMovementTimer += Time.deltaTime;
                if (noMovementTimer >= NO_MOVEMENT_THRESHOLD)
                {
                    rb.gravityScale = 1;
                }
            }
            else
            {
                noMovementTimer = 1f;
            }

            // 正常水平移动
            float newX = Mathf.MoveTowards(
                transform.position.x, 
                targetX, 
                moveSpeed * Time.deltaTime);
            transform.position = new Vector3(
                newX, 
                transform.position.y, 
                transform.position.z);
        }
    }
}